#!/bin/zsh

GIT_USER_NAME=$(id -nu)
GIT_USER_EMAIL=chenxinliang@bit.edu.cn

setup_git() {
	git config --global alias.st status
	git config --global alias.ci commit
	git config --global alias.co checkout
	git config --global alias.br branch
	git config --global user.name ${GIT_USER_NAME}
	git config --global user.email ${GIT_USER_EMAIL}
	git config --global --add safe.directory /srv
}


install_git() {
	GIT_INSTALLED=`apk list git`
	if [ -z ${GIT_INSTALLED} ]; then
		apk add git
	fi
	setup_git
}

install_lazygit() {
	LAZYGIT_INSTALLED=`which lazygit`
	if [ -z ${LAZYGIT_INSTALLED} ]; then
		LAZY_GIT_URL=https://github.com/jesseduffield/lazygit/releases/download/v0.41.0/lazygit_0.41.0_Linux_x86_64.tar.gz
		LAZY_GIT_MIRROR_URL=https://master.dl.sourceforge.net/project/lazygit.mirror/v0.41.0/lazygit_0.41.0_Linux_x86_64.tar.gz?viasf=1
		LAZY_GIT_FILENAME=lazygit_0.41.0_Linux_x86_64.tar.gz
		LAZY_GIT_INSTALLER_PATH=/tmp/${LAZY_GIT_FILENAME}
		if [ -f ${LAZY_GIT_INSTALLER_PATH} ]; then
			CURL_INSTALED=`apk list curl`
			if [ -z ${CURL_INSTALED} ]; then
				apk add curl
			fi
			curl -o /tmp/${LAZY_GIT_FILENAME} ${LAZY_GIT_MIRROR_URL}
		fi
		if [ -f ${LAZY_GIT_INSTALLER_PATH} ]; then
			tar zxf ${LAZY_GIT_INSTALLER_PATH} -C /usr/local/share/lazygit
			mv /usr/local/share/lazygit/lazygit /usr/local/bin/lazygit
		else
			echo "File ${LAZY_GIT_FILENAME} not found in /tmp/!"
		fi
	fi
}

install_git
install_lazygit

# vim: ff=unix ts=4 sw=4
